#include "LCommon.h"

void Database::Connect(const char* host, const char* user, const char* pw, const char* db, ui32 port)
{
	if(!mysql_real_connect(m_connection, host, user, pw, db, port, NULL, 0))
		Log->Notice("MySQL", "Could not connect to database %s: %s", db, mysql_error(m_connection));
}

void Database::Query(const char* query, ...)
{
	va_list ap;
	va_start(ap, query);
	char* sql = new char[1024];
	vsprintf(sql, query, ap);
	if(mysql_query(m_connection, sql))
		Log->Notice("MySQL", "%s", mysql_error(m_connection));
	delete [] sql;
}

QResult* Database::SafeQuery(const char* query, ...)
{
	va_list ap;
	va_start(ap, query);
	char* sql = new char[1024];
	vsprintf(sql, query, ap);
	if(mysql_query(m_connection, sql))
	{
		Log->Notice("MySQL", "%s", mysql_error(m_connection));
		return NULL;
	}
	delete [] sql;
	m_result = mysql_store_result(m_connection);
	QResult* rs = new QResult(m_result);
	return rs;
}